package com.lei.chinese_herbal_medicine.Util;
import org.python.antlr.ast.Str;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
public class JavaOperatePythonUtil {


    /***
     * 调用python脚本训练或增强模型
     * @return 是否执行完毕，true 完毕 false 错误
     */
    public static boolean createOrReinforceModel(){
        boolean flag=true;
        try {
            // 定义 Python 脚本的路径
            String pythonScriptPath = "E:\\PytorchLearning\\pythonProject\\chineseHerbalMedicineIdentificationSystem\\IdentificationSystem.py";

            // 定义 Anaconda 环境中的 Python 解释器路径
            String pythonInterpreterPath = "F:\\anaconda3\\envs\\pytorch\\python.exe";

            // 构建命令列表
            ProcessBuilder processBuilder = new ProcessBuilder(
                    pythonInterpreterPath,
                    pythonScriptPath
                    // 添加更多参数...
            );

            // 设置工作目录（可选）
            processBuilder.directory(new File("E:\\PytorchLearning\\pythonProject\\chineseHerbalMedicineIdentificationSystem\\"));

            // 启动进程
            Process process = processBuilder.start();

            // 获取标准输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println("Python Output: " + line);
            }

            // 获取标准错误输出
            BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            while ((line = errorReader.readLine()) != null) {
                System.out.println("Python Error: " + line);
            }

            // 等待进程执行结束
            int exitCode = process.waitFor();
            if (exitCode>0)flag=false;
            System.out.println("Python script exited with code " + exitCode);

        } catch (IOException | InterruptedException e) {
            flag=false;
            e.printStackTrace();
        }
        return flag;
    }


    /***
     * 传入待识别的图片路径，获取该中草药的名字
     * @param targetPath 图片的绝对路径
     * @return 中草药的拉丁名
     */
    public static String recognitionImage(String targetPath){
        String lab=null;
        try {
            // 定义 Python 脚本的路径
            String pythonScriptPath = "E:\\PytorchLearning\\pythonProject\\chineseHerbalMedicineIdentificationSystem\\testSystem.py";

            // 定义 Python 脚本的参数
            String functionName = "test_herb";

            // 构建命令列表
            ProcessBuilder processBuilder = new ProcessBuilder(
                    "F:\\anaconda3\\envs\\pytorch\\python.exe",
                    pythonScriptPath,
                    functionName,
                    targetPath
            );

            // 设置工作目录（可选）
            processBuilder.directory(new File("E:\\PytorchLearning\\pythonProject\\chineseHerbalMedicineIdentificationSystem\\"));

            // 启动进程
            Process process = processBuilder.start();

            // 获取标准输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                lab = line;
                System.out.println("Python Output: " + line);
            }

            // 获取标准错误输出
            BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            while ((line = errorReader.readLine()) != null) {
                System.out.println("Python Error: " + line);
            }

            // 等待进程执行结束
            int exitCode = process.waitFor();
            System.out.println("Python script exited with code " + exitCode);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
        return lab;
    }
}
